package com.ds.infrastructure.hyperspace.console.service.impl;

import com.ds.infrastructure.hyperspace.console.dao.dao.TbDeployInstallTaskDao;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbDeployInstallTaskEnum;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbPublicEnum;
import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbDeployInstallTask;
import com.ds.infrastructure.hyperspace.console.entity.where.TbCustomWhere;
import com.ds.infrastructure.hyperspace.console.entity.where.TbDeployTaskCustomWhere;
import com.ds.infrastructure.hyperspace.console.exceptions.ArgumentValidException;
import com.ds.infrastructure.hyperspace.console.service.inf.IDeployInstallTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-10-23 10:05
 **/

@Service
public class DeployInstallTaskService implements IDeployInstallTaskService {
    @Autowired
    TbDeployInstallTaskDao tbDeployInstallTaskDao;

    @Override
    public int save(TbDeployInstallTask record) {
        return tbDeployInstallTaskDao.insertSelective(record);
    }

    @Override
    public int update(TbDeployInstallTask record) {
        return tbDeployInstallTaskDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public TbDeployInstallTask get(Long id) {
        TbDeployInstallTask entity = tbDeployInstallTaskDao.selectByPrimaryKey(id);
        if (entity != null && TbPublicEnum.GMD_IS_DEL.NO.code.equals(entity.getGmtIsDeleted())) {
            return entity;
        }
        return null;
    }

    @Override
    public int delete(Long id) {
        TbDeployInstallTask item = new TbDeployInstallTask();
        item.setId(id);
        item.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.YES.code);
        return update(item);
    }

    @Override
    public List<TbDeployInstallTask> getList(TbDeployInstallTask where, TbCustomWhere customWhere, TbPageWhere pageWhere) {
        return tbDeployInstallTaskDao.selectListForPage(where, customWhere, pageWhere);
    }

    @Override
    public int getTotal(TbDeployInstallTask where, TbCustomWhere customWhere) {
        return tbDeployInstallTaskDao.selectListForPageCount(where, customWhere);
    }

    @Override
    public TbDeployInstallTask selectOne(TbDeployInstallTask where, TbCustomWhere customWhere) {
        List<TbDeployInstallTask> itemList = tbDeployInstallTaskDao.selectListForPage(where, customWhere, TbPageWhere.newPage4OnlyOne());
        return itemList.stream().findFirst().orElseGet(() -> null);
    }

    @Override
    public TbDeployInstallTask getOrThrow(Long id) {
        return getOrThrow(id, null);
    }

    @Override
    public TbDeployInstallTask getOrThrow(Long id, String exceptionTitle) {

        TbDeployInstallTask entity = get(id);
        if (entity == null) {
            String error = String.format("%s:没有找到对应的记录;当前id=%s", exceptionTitle == null ? "获取TbDeployInstallTask信息失败" : exceptionTitle, id);
            throw new ArgumentValidException(error);
        }
        return entity;
    }

    @Override
    public TbDeployInstallTask selectLastOneForExecuteSuccess(Long stageId, Long environmentId) {
        TbDeployInstallTask where = new TbDeployInstallTask();
        where.setStageId(stageId);
        where.setEnvironmentId(environmentId);
        where.setExecuteState(TbDeployInstallTaskEnum.EXECUTE_STATE.SUCCESS.code);
        where.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);
        TbDeployTaskCustomWhere customWhere = new TbDeployTaskCustomWhere().setOrderByType(TbPublicEnum.ORDER_BY_TYPE.ID_DESC.value);
        return selectOne(where, customWhere);
    }
}
